#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QFileDialog>
#include <QSaveFile>
#include <QFile>
#include <QDir>
#include <QException>
#include <QMessageBox>
#include <QTextBlock>
#include <QTextStream>

typedef enum {
    kLineSeperatorStyleWin = 0,
    kLineSeperatorStyleUnix
} LineSeperatorStyle;

namespace {
QString getLineSeperator(LineSeperatorStyle style = kLineSeperatorStyleUnix)
{
    QString res = "\n";
    switch (style) {
    case kLineSeperatorStyleWin:
        res = "\r\n";
        break;
    case kLineSeperatorStyleUnix:
        res = "\n";
        break;
    default:
        ;
    }
    return res;
}

}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actOpen_IODevice_triggered()
{
    // 用只读的方式打开一个文件
    QFile file(getFile());
    if (!file.exists()) {
        return;
    }
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return;
    }

    ui->textEditDeivce->appendPlainText(file.readAll());
    file.close();
}

QString MainWindow::getFile(bool save)
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString dlgTitle = "打开一个文件";
    QString filter = "程序文件(*.h *.c *.cpp);;文本文件(*.txt);;所有文件(*.*)";
    QString fileName;

    if (save) {
        fileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath, filter);
    }
    else {
        fileName = QFileDialog::getOpenFileName(this, dlgTitle, curPath, filter);
    }

    if (!fileName.isEmpty()) {
        QFileInfo fileInfo(fileName);
        QDir::setCurrent(fileInfo.absoluteFilePath());
    }

    return fileName;
}


void MainWindow::on_actSave_IODevice_triggered()
{
    QFile file(getFile(true));
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        return;
    }

    QString str = ui->textEditDeivce->toPlainText();
    file.write(str.toUtf8(), str.toUtf8().length());

    file.close();
}


void MainWindow::on_actSave_TextSafe_triggered()
{
    QSaveFile file(getFile(true));
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        return;
    }

    file.setDirectWriteFallback(false);
    try {
        QString str = ui->textEditDeivce->toPlainText();
        file.write(str.toUtf8(), str.toUtf8().length());
        file.commit();
    }
    catch(QException &ex) {
        QMessageBox::warning(this, "异常", "保存文件过程方式错误!");
        file.cancelWriting();
    }
}


void MainWindow::on_actOpen_TextStream_triggered()
{
    // 用只读的方式打开一个文件
    QFile file(getFile());
    if (!file.exists()) {
        return;
    }
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return;
    }

    QTextStream stream(&file);
    stream.setAutoDetectUnicode(true);
    while (!stream.atEnd()) {
        QString line = stream.readLine();
        ui->textEditStream->appendPlainText(line);
    }

    file.close();
}

void MainWindow::on_actSave_TextStream_triggered()
{
    QSaveFile file(getFile(true));
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        return;
    }

    file.setDirectWriteFallback(false);
    try {
        QTextStream stream(&file);
        stream.setAutoDetectUnicode(true);

        auto *doc = ui->textEditStream->document();
        for (int i = 0; i < doc->blockCount(); ++i) {
            stream << doc->findBlockByNumber(i).text();
            stream << getLineSeperator();
        }
        file.commit();
    }
    catch(QException &ex) {
        QMessageBox::warning(this, "异常", "保存文件过程方式错误!");
        file.cancelWriting();
    }
}

